package com.woniuxy.io.dir;

import java.io.File;

/**
 * description: DirDemo02 <br>
 * date: 2022/2/4 9:57 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

/**
 * 使用面向对象统计文件的大小
 */
public class DirCount {
    //文件夹大小
    private long len;
    //文件夹路径
    private String path;
    //文件的个数
    private int fileSize = -2;
    //目录的个数
    private int dirSize;
    //源
    private File src;

    public DirCount(String path) {
        this.path = path;
        this.src = new File(path);
        conut(this.src);
    }

    public static void main(String[] args) {
        DirCount dir = new DirCount("/Users/shesaifei/Desktop/ce");
        System.out.println(dir.getLen() + "," + "文件的个数:" + dir.fileSize + "," + "目录的个数：" + dir.dirSize);

    }

    //统计大小
    private void conut(File src) {
        //获取大小
        if (null != src && src.exists()) {
            if (src.isFile()) {
                //大小
                len += src.length();
                this.fileSize++;
            } else {
                this.dirSize++;
                //获取子目录
                for (File files : src.listFiles()) {
                    conut(files);
                }
            }
        }
    }

    public long getLen() {
        return len;
    }

    public int getFileSize() {
        return fileSize;
    }

    public int getDirSize() {
        return dirSize;
    }
}
